<?php
class Application_Model_Filter_OrderSearch {
    //快速搜索
    public $quick;
    //开始时间
    public $starttime;
    //结束时间
    public $endtime;
    //购买账户
    public $taobaoid;
    //状态
    public $status;
    //用户id
    public $userid;
    //今日
    public $day;
    //我受理的
    public $myorder;
    //单项退货
    public $single;
    //问题代购
    public $problem;
    /**
     * 代购异常
     */
    public $abnormal;
    /**
     * 是否删除
     */
    public $isdelete;
    /**
     * 时间方式
     */
    public $seltime;
    /**
     * 排序方式
     */
    public $order;
    /**
     * 受理人
     */
    public $operatorname;
    /**
     * 查询类型
     */
    public $quickType;
    /**
     * 退换货状态
     */
    public $rechangeStatus;
    
    public $noRechangeStatus;
    
    
    function getSearchFileds() {
        $where = array();
        $and = "";
        $isProblem = 1;
        $isdelete = 1;
        $isMyOrder = 1;
        $storeno = "";
        $id = "";
        $this->order = ' datePaid desc ';
        if ($this->quick != "") {
            $firstLetter = substr($this->quick, 0, 1);
            if (strtoupper($firstLetter) == "F") {
                $storeno = "P" . strtoupper(substr($this->quick, 1, 5));
            } else if (strtoupper($firstLetter) == "D"){
                $id = substr($this->quick, 1);
            } else {
                $storeno = $this->quick;
            }
            $and = "(";
            if($storeno){
                $userItem = Application_Model_User::getByNikeNameStore($storeno);
                if ($userItem != null) {
                    $inuserid = "(";
                    foreach ($userItem as $userval) {
                        $inuserid.= $userval->id . ",";
                    }
                    $inuserid.= "0)";
                    $where[$and . " userId in "] = $inuserid;
                    $and = " or ";
                } else {
                    $where[$and . " userId = "] = $this->quick;
                    $and = " or ";
                }
            }
            if($id){
                $where[$and . " id = "] = $id;
                $and = " or ";
            }
            if (strstr($this->quick, "U") || strstr($this->quick, "u")) {
                $orderno = substr($this->quick, 5, strlen($this->quick) - 6);
            } else {
                $orderno = $this->quick;
            }
            $orItem = Application_Model_OrderItem::getOrderIdByProductName($this->quick,$orderno);
            if ($orItem != null) {
                $pid = "(";
                foreach ($orItem as $orval) {
                    $pid.= $orval->orderId . ',';
                }
                $pid.= "0)";
                $where[$and . " id in "] = $pid;
                $and = " or ";
            } else {
                $where[$and . " id = "] = $orderno;
                $and = " or ";
            }
            $trackItem1 = Application_Model_Tracking::getOneByObjectTypeOperateTypeContent(Application_Model_DomainobjectType::ObjectType_Order, Application_Model_Tracking::Type_ReChangeLogistics,$this->quick);
            if($trackItem1){
                $where[$and . " id = "] = $trackItem1->objectId;
                $and = " or ";
            }
            $trackItem = Application_Model_Tracking::getOneByObjectTypeOperateTypeContent(Application_Model_DomainobjectType::ObjectType_Order, Application_Model_Tracking::Type_ReissueLogistics,$this->quick);
            if($trackItem){
                $where[$and . " id = "] = $trackItem->objectId;
                $and = " or ";
            }
            $where[$and . " procureNo like "] = "%" . $this->quick . "%";
            $where[$and . " logisticsNo like "] = "%" . $this->quick . "%";
            $where[" ) and "] = 1;
            $and = " and ";
            $isProblem = 0;
            $isdelete != 1;
        }
        if($this->seltime == "paidTime"){
            if ($this->starttime != "") {
                $where[$and . " datePaid >= "] = $this->starttime;
                $and = " and ";
            }
            if ($this->endtime != "") {
                $where[$and . " datePaid <= "] = $this->endtime." 23:59:59";
                $and = " and ";
            }
        } else {
            if ($this->starttime != "") {
                $where[$and . " dateCreated >= "] = $this->starttime;
                $and = " and ";
            }
            if ($this->endtime != "") {
                $where[$and . " dateCreated <= "] = $this->endtime." 23:59:59";
                $and = " and ";
            }
        }
        if ($this->status != "") {
            if(is_array($this->status)){
                $statusStr = $this->StatusAnalysis($this->status);
                $where[$and . " status not in "] = $statusStr;
                $and = " and ";
                $this->order = " datePaid asc";
            }else{
                $where[$and . " status = "] = $this->status;
                $and = " and ";
                $this->order = " datePaid asc";
            }
            if($this->status == Application_Model_Order::STATUS_OUTTIME_UNSEND || $this->status == Application_Model_Order::STATUS_OUTTIME_INSTORE){
                $this->order = " dateConfrim asc";
            }
            if($this->status == Application_Model_Order::STATUS_PAYING) {
                $this->order = " dateCreated desc ";
            }
            if($this->status == Application_Model_Order::STATUS_PAID) {
                $isMyOrder = 0;
            }
        }
        if ($this->userid != null) {
            $where[$and . " userId = "] = $this->userid;
            $and = " and ";
            $isProblem = 0;
            $this->order = " dateCreated desc ";
        }
        if ($this->day == 1){
            $where[$and . " dateCreated >= "] = date("Y-m-d")." 00:00:00";
            $and = " and ";
        }
        if ($this->myorder != "" && $isMyOrder){
            $where[$and." operator = "] = $this->myorder;
            $and = " and ";
        }
        if ($this->single == 1){
            $orderItem = Application_Model_OrderItem::getSingleAll();
            $orderId = "(";
            if($orderItem){
                foreach ($orderItem as $orderVal) {
                    $orderId .= $orderVal->orderId.",";
                }
            }
            $orderId .= "0)";
            $where[$and . " id in "] = $orderId;
            $and = " and ";
            $where[$and . " status not in "] = "(".Application_Model_Order::STATUS_CANCEL.",".Application_Model_Order::STATUS_BANK.")";
        }
        if($isProblem == 1){
            if($this->problem != ""){
                $where[$and." problem = "] = $this->problem;
                $and = " and ";
                $this->order = " dateConfrim asc";
            }
        }
        
        if($this->taobaoid > 0){
            $where[$and . " procureNo like "] = "%" . $this->taobaoid;
            $and = " and ";
        }
        
        if($isdelete == 1){
            if($this->isdelete == 1){
                $where[$and." isdelete = "] = 1;
                $and = " and ";
            } else {
                $where[$and." isdelete != "] = 1;
                $and = " and ";
            }
        }
        if($this->quickType == "rechangeStatus"){
            $where[$and . " (scheme = ".Application_Model_Order::Scheme_Reprocess." or isAbnormal in "] = "(".Application_Model_Order::Abnormal_Less.",".Application_Model_Order::Abnormal_Error.",".Application_Model_Order::Abnormal_Damage."))";
            $and = " and ";
        } 
        if($this->quickType == "finStatus"){
            $where[$and." (scheme = 0 or scheme is null) and 1 = "] = 1;
            $and = " and ";
            $isProblem = 0;
        }
        if($this->rechangeStatus != ""){
            $where[$and." rechangeStatus = "] = $this->rechangeStatus;
            $and = " and ";
            $this->order = " dateConfrim asc";
        }
        if($this->rechangeStatus != ""){
            $where[$and." rechangeStatus = "] = $this->rechangeStatus;
            $and = " and ";
            $this->order = " dateConfrim asc";
        }
        if($this->noRechangeStatus != "" && $isProblem == 1){
            $where[$and." (rechangeStatus is null or rechangeStatus != "] = $this->noRechangeStatus;
            $where[" ) and 1 = "] = 1;
            $and = " and ";
        }
        return $where;
    }
    
    function getOrder()
    {
        $this->getSearchFileds();
        return $this->order;
    }
    
    private function StatusAnalysis($statusAll) {
            $statusStr = "(";
            foreach ($statusAll as $statusVal) {
                  $statusStr .= $statusVal . ",";
            }
            $statusStr .= "0)";
            return $statusStr;
      }

}
